package com.un.ebs.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.un.ebs.core.util.StringUtils;
import com.un.ebs.product.domain.ProductClass;
import com.un.ebs.product.domain.ProductClassOrderAttr;
import com.un.ebs.product.dto.ProductClassOrderAttrAddDto;
import com.un.ebs.product.dto.ProductClassOrderAttrUpdateDto;
import com.un.ebs.product.dto.SortChangeDto;
import com.un.ebs.product.mapper.ProductClassMapper;
import com.un.ebs.product.mapper.ProductClassOrderAttrMapper;
import com.un.ebs.product.service.ProductClassOrderAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductClassOrderAttrServiceImpl extends ServiceImpl<ProductClassOrderAttrMapper, ProductClassOrderAttr> implements ProductClassOrderAttrService {

    @Autowired
    private ProductClassMapper productClassMapper;

    @Override
    public void add(ProductClassOrderAttrAddDto req) {
        ProductClassOrderAttr obj = new ProductClassOrderAttr();
        obj.setName(req.getName());
        obj.setClassId(req.getClassId());
        obj.setDescr(req.getDesc());
        obj.setGroupId(req.getGroupId());
        obj.setIsSubmitRequired(req.isSubmitRequired());
        obj.setIsAcceptRequired(req.isAcceptRequired());
        obj.setIsDelete(false);
        obj.setIsMulti(req.isMulti());
        obj.setIsMultiToOne(req.isMultiToOne());
        obj.setSort(baseMapper.maxSort());
        obj.insert();
    }

    @Override
    public void update(ProductClassOrderAttrUpdateDto req) {
        ProductClassOrderAttr obj = getById(req.getId());
        obj.setName(req.getName());
        obj.setClassId(req.getClassId());
        obj.setDescr(req.getDesc());
        obj.setGroupId(req.getGroupId());
        obj.setIsSubmitRequired(req.isSubmitRequired());
        obj.setIsAcceptRequired(req.isAcceptRequired());
        obj.setIsMulti(req.isMulti());
        obj.setIsMultiToOne(req.isMultiToOne());
        obj.updateById();
    }

    @Override
    public ProductClassOrderAttr queryById(String id) {
        return getBaseMapper().selectById(id);
    }

    @Override
    public void delete(String id) {
        baseMapper.deleteById(id);
    }

    @Override
    public void changeSort(String id, int index) {
        ProductClassOrderAttr obj = getById(id);
        obj.setSort(index);
        obj.updateById();
    }

    @Override
    public void changeSort(List<SortChangeDto> req) {
        for (SortChangeDto reqObj : req) {
            ProductClassOrderAttr obj = getById(reqObj.getId());
            obj.setSort(reqObj.getIndex());
            obj.updateById();
        }
    }

}
